package SupermarketManageSystem;

import org.apache.commons.codec.digest.DigestUtils;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

public class Method {
    Scanner sc = new Scanner(System.in);
    List<Vip> vipList = new ArrayList<>();
    Vip vip = new Vip();
    public void showUserCommand(){
        while (true) {
            System.out.println("*************欢迎进入超市会员功能模块*************");
            System.out.print("1、积分积累\t");
            System.out.print("2、积分兑换\t");
            System.out.print("3、查询剩余积分\t");
            System.out.print("4、修改密码\t");
            System.out.println("5、返回主菜单");
            System.out.println("**********************************************");
            System.out.println("请选择：");
            int command = sc.nextInt();
            switch (command){
                case 1:
                    accumulateIntegral();
                    break;
                case 2:
                    integralExchange();
                    break;
                case 3:
                    queryIntegral();
                    break;
                case 4:
                    modifyPassWord();
                    break;
                case 5:
                    //返回主菜单
                    System.out.println("返回主菜单成功！");
                    return;
                default:
                    System.out.println("您输入的操作命令不正确！");
            }
        }
    }

    private void modifyPassWord() {
        System.out.println("请输入旧密码：");
        String oldPassWord = sc.next();
        if (oldPassWord.equals(vip.getPassword())){
            System.out.println("请输入新密码：");
            String newPassWord = sc.next();
            if (newPassWord.length() >= 6 && !newPassWord.equals(oldPassWord)){
                vip.setPassword(newPassWord);
                System.out.println("密码修改成功！");
            } else {
                System.out.println("请检查密码是否大于等于6位且新密码和旧密码是否相同！");
            }

        } else {
            System.out.println("旧密码错误，修改密码失败！");
        }
    }

    private void queryIntegral() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDate = dateFormat.format(vip.getDate());
        System.out.println("会员信息：");
        System.out.println("会员ID：" + vip.getCardNo());
        System.out.println("会员姓名：" + vip.getName());
        System.out.println("会员注册日期：" + formattedDate);
        System.out.println("当前积分为：" + vip.getIntegral());
    }

    private void integralExchange() {
        System.out.println("请输入您需要兑换使用的积分：");
        int integralQuantity = sc.nextInt();
        if (integralQuantity <= vip.getIntegral()){
            int notRedeemedIntegral = integralQuantity % 100;
            double exchangeMoney = 0.0;
            if (integralQuantity >= 100){
                exchangeMoney = Math.floor(integralQuantity / 100) * 0.1;
            }
            DecimalFormat df = new DecimalFormat("0.00");//创建了一个DecimalFormat对象，用于格式化兑换金额。
            String formattedAmount = df.format(exchangeMoney);// 这行代码使用DecimalFormat对象将兑换金额格式化为保留两位小数的字符串。
            System.out.println("您的消费金额中使用的积分抵消：" + exchangeMoney + "元");
            int updateIntegral = vip.getIntegral() - integralQuantity + notRedeemedIntegral;
            vip.setIntegral(updateIntegral);
            System.out.println("积分兑换成功！");
        } else {
            System.out.println("积分不足，无法兑换！");
        }
    }

    private void accumulateIntegral() {
        System.out.println("请输入本次的消费金额：");
        double monetary = sc.nextDouble();
        double ratio = 1.0;
        int integral = vip.getIntegral() + (int)(Math.round(monetary * ratio));
        vip.setIntegral(integral);
        System.out.println("积分新增成功！");
    }

    public void register(){
        vip = new Vip();
        System.out.println("-----系统开卡操作-----");
        System.out.println("请输入会员姓名：");
        String name = sc.next();
        vip.setName(name);
        while (true) {
            System.out.println("请输入密码：");
            String passWord = sc.next();
            if (passWord.length() < 6){
                System.out.println("密码长度不能少于6位，请重新输入！");
                continue;
            }
            String md5Str = DigestUtils.md5Hex(passWord);//对密码进行MD5加密
            System.out.println("请您再次输入密码：");
            String okPassWord = sc.next();
            if (DigestUtils.md5Hex(okPassWord).equals(md5Str)){//比较密文
                vip.setPassword(okPassWord);
                break;
            } else {
                System.out.println("您输入的两次密码不一致，请重新确认");
            }
        }
        //获取当前时间
        Date currentTime = new Date();
        vip.setDate(currentTime);
        //生成8位唯一的随机卡号
        String cardNo = getRandomCardNum();
        vip.setCardNo(cardNo);
        vip.setIntegral(100);
        vipList.add(vip);
        System.out.println("恭喜,开通会员卡成功，系统赠送您100积分！您的会员卡号为：" + cardNo);
    }

    public void login(){
        System.out.println("-----系统登录操作-----");
        if (vipList.size() == 0){
            System.out.println("对不起，当前系统无任何会员，请先开卡，再登录！");
            return;
        }
        System.out.println("请您输入卡号：");
        String cardNo = sc.next();
        Vip vip = getVipByCardNum(cardNo,vipList);
        if (vip != null){
            while (true) {
                System.out.println("请您输入密码：");
                String passWord = sc.next();
                if (vip.getPassword().equals(passWord)){
                    System.out.println("恭喜您，" + vip.getName() + "进入系统" + "您的账户为：" + vip.getCardNo());
                    showUserCommand();
                    return;
                } else {
                    System.out.println("您输入的密码有误！");
                }
            }
        } else {
            System.out.println("对不起，系统中不存在该账户！");
        }
    }
    public String getRandomCardNum(){
        String cardNo = "";
        Random r = new Random();
        for (int i = 0; i < 8; i++) {
            cardNo += r.nextInt(10);
        }
        Vip vip = getVipByCardNum(cardNo, vipList);
        if (vip == null){
            return cardNo;
        }
        return null;
    }

    private Vip getVipByCardNum(String cardNo, List<Vip> vipList) {
        for (int i = 0; i < vipList.size(); i++) {
            Vip vip = vipList.get(i);
            if (vip.getCardNo().equals(cardNo)){
                return vip;
            }
        }
        return null;
    }
}
